package com.hiedu.service.impl;

import com.hiedu.beans.dto.ProductAddDTO;
import com.hiedu.beans.dto.ProductUpdateDTO;
import com.hiedu.beans.entity.Product;
import com.hiedu.commons.ServiceCode;
import com.hiedu.commons.ServiceException;
import com.hiedu.mapper.ProductMapper;
import com.hiedu.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service //业务注解
@Transactional //事务注解
@Slf4j
public class ProductServiceImpl implements ProductService {
    // 自动注入商品持久层对象
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Product> getProductList() {
        List<Product> productList = productMapper.getProductList();
        if(productList==null || productList.size()==0){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"商品表中没有商品信息");
        }
        return productList;
    }


    //id=2
    @Override
    public Product getById() {
        //调用持久层方法
        Product product = productMapper.getById(1001);
        if(product==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"商品不存在");
        }
        return product;
    }

    @Override
    public Product getById2(Integer id) {
        log.debug("查询商品的编号:{}",id);
        if (id == null) {
            throw new RuntimeException("商品id不能为空");
        }
        if(id<1){
            throw new RuntimeException("商品编号参数有误");
        }
        Product product = productMapper.getById2(id);
        if(product==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"商品不存在");
        }
        return product;
    }

    @Override
    public void delete(Integer id) {
        if(id==null){
            throw new RuntimeException("商品编号不能为空");
        }
        //根据编号去查询商品
        Product product = productMapper.getById2(id);
        //判断商品是否存在:代码的健壮性
        if(product == null || product.getId() == null){
            throw new RuntimeException("商品不存在");
        }
        //返回受影响的行数
// alt + shift + Z
        int result = 0;
        try {
            result = productMapper.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(result < 1){
            throw  new RuntimeException("删除商品失败");
        }
    }

    @Override
    public void add(ProductAddDTO productAddDTO) {
        // todo 对新增商品的数据进行合法性校验，后期使用校验框架完成
//将新增的数据筋拆分，封装到实体类中
        Product product  =new Product();
        product.setName(productAddDTO.getName());
        product.setPrice(productAddDTO.getPrice());
        product.setNum(productAddDTO.getNum());
        //进行新增操作
        int result = productMapper.insert(product);
        if(result < 1){
            throw new RuntimeException("新增商品失败");
        }



    }

    @Override
    public void update(ProductUpdateDTO productUpdateDTO) {

        // todo 数据的合法性校验
        if(productUpdateDTO == null || productUpdateDTO.getId()==null){
            throw new RuntimeException("修改商品的数据不能为空");
        }
        //构建Product实体类对象
        Product product = new Product();
        product.setName(productUpdateDTO.getName()); // ctrl + alt + 向下箭头
        product.setPrice(productUpdateDTO.getPrice());
        product.setNum(productUpdateDTO.getNum());
        product.setId(productUpdateDTO.getId());
        int result = 0;
        try {
            result = productMapper.update(product);
        } catch (Exception e) {
            throw new RuntimeException("修改商品：执行SQL语句失败");
        }
        if(result < 1){
            throw new RuntimeException("修改商品失败");
        }
    }





}
